﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BUS;
using DTO;




namespace GUI
{
    public partial class FormDanhSachNguoiDung : Form
    {
        public FormDanhSachNguoiDung()
        {
            InitializeComponent();

        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        
        private void chkChonTatCa_CheckedChanged(object sender, EventArgs e)
        {
            if(chkChonTatCa.Checked)
            {
                foreach (DataGridViewRow r in gridViewXemDanhSachNguoiDung.Rows)
                {
                    r.Cells[2].Value = true;
                }
            }
            else if (!(chkChonTatCa.Checked))
            {
                foreach (DataGridViewRow r in gridViewXemDanhSachNguoiDung.Rows)
                {
                    r.Cells[2].Value = false;
                }
            }
            
        }    
        private void FormDanhSachNguoiDung_Load(object sender, EventArgs e)
        {
            List<TaiKhoanDTO> danhsach = new List<TaiKhoanDTO>();
            TaiKhoanBUS bus = new TaiKhoanBUS();
            danhsach = bus.XemDanhSachNguoiDung();
            gridViewXemDanhSachNguoiDung.Rows.Clear();
            for (int i = 0; i < danhsach.Count; i++)
            {
                gridViewXemDanhSachNguoiDung.Rows.Add(danhsach[i].Id_Tai_Khoan, danhsach[i].User_Name);
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            
            DialogResult dialog = MessageBox.Show("Bạn Chắc Chắn Muốn Xóa Tài Khoản Này?", "Xóa Tải Khoản", MessageBoxButtons.YesNo);
            if(dialog == DialogResult.Yes)
            {
                foreach (DataGridViewRow singleRow in gridViewXemDanhSachNguoiDung.Rows)
                {
                    if (singleRow.Cells[2].Value != null)
                    {
                        if ((Boolean)singleRow.Cells[2].Value == true)
                        {
                            TaiKhoanBUS tk = new TaiKhoanBUS();
                            tk.XoaTaiKhoan(Convert.ToInt32(singleRow.Cells[0].Value));
                            gridViewXemDanhSachNguoiDung.Rows.RemoveAt(singleRow.Cells[2].RowIndex);
                        }
                    }
                }
            }
        }

        private void btnThemNguoiDung_Click(object sender, EventArgs e)
        {
            FormDangKySuDung frm = new FormDangKySuDung();
            frm.Show();
        }
        
        private void gridViewXemDanhSachNguoiDung_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
  
        }

        private void btnPrefesh_Click(object sender, EventArgs e)
        {
            this.Refresh();
        }
        public static string strSend = "";
        public static int ID_TK = 0;
        private void btnSua_Click(object sender, EventArgs e)
        {
            int chkDem = 0;
            foreach (DataGridViewRow singleRow in gridViewXemDanhSachNguoiDung.Rows)
            {
                if (singleRow.Cells[2].Value != null)
                {
                    if ((Boolean)singleRow.Cells[2].Value == true)
                    {
                        chkDem = chkDem + 1;
                    }
                }
            }
            if(chkDem ==0 || chkDem > 1)
            {
                MessageBox.Show("Chỉ Được Chọn 1 Tài Khoản Để Tiến Hành Sửa Đổi!");
                return;
            }
            
            foreach (DataGridViewRow singleRow in gridViewXemDanhSachNguoiDung.Rows)
            {
                if (singleRow.Cells[2].Value != null)
                {
                    if ((Boolean)singleRow.Cells[2].Value == true)
                    {
                        ID_TK = Convert.ToInt32(singleRow.Cells[0].Value.ToString());
                        strSend = singleRow.Cells[1].Value.ToString();
                    }
                }
            }
            FormCapNhapNguoiDung f = new FormCapNhapNguoiDung();
            f.ShowDialog();
        }
        

    }
}
